#!/bin/bash
# passgen.sh
#
# Generates a random password of LENGTH chars

# Chars that could compose the password
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

# Check if the length parameter has been set
if [ ! -z $1 ]; then
	LENGTH="$1"
else
	LENGTH="8"
fi

# Loop to add chars to the password
while [ "${n:=1}" -le "$LENGTH" ]
do
	# Returns expansion of MATRIX at random position, by length 1
	# (Quite tricky, huh?)
	PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
	
	# Next char, please!
	let n+=1
done

echo "$PASS"

exit 0
